<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$worker = pdo_get('jy_yiliao_worker',array('uniacid' => $_SESSION['uniacid'], 'mid' => $_SESSION['uid']));
	if(empty($worker)){
		exi('您不是大夫，不能核销!','error','jy_yiliao/index/');
	}
	$id = trim($_RQ['id']);
	if(empty($id)){
		exi('请扫码正常的核销码!','error','jy_yiliao/worker.index/');
	}
	$item = pdo_get('jy_yiliao_worker_goods_order',array('uniacid' => $_SESSION['uniacid'], 'id' => $id));
	if (empty($item)){
		exi('未查询到记次时商品或核销码已失效,请核对核销码!', 'error','jy_yiliao/worker.index/');
	}
	if($item['status']!=1){
		exi('当前订单，不能核销！','error','jy_yiliao/worker.index/');
	}
	if($worker['id']!=$item['workerid']){
		exi('当前订单商品项目不属于您，不能核销！','error','jy_yiliao/worker.index/');
	}
	if($item['lasttimes']<=0){
		pdo_update('jy_yiliao_worker_goods_order',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
		exi('当前订单使用次数已用完，不能核销！','error','jy_yiliao/worker.index/');
	}
	$status=array('待支付','已支付','已使用');
	$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
	$item['ispaytitle']=$item['ispay']?'已支付':'未支付';
	$item['statustitle']=$status[$item['status']];
	$item['goods']=pdo_get('jy_yiliao_worker_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goods_id']),array('title','image','id'));
	$return['post']=array(
		'orderid'=>$item['id'],
		'lasttimes'=>$item['lasttimes'],
		'num'=>1,
	);
	if($item['storeid']){
		$store=pdo_get('jy_yiliao_store',array('uniacid' => $_SESSION['uniacid'], 'id' => $item['storeid']),array('id','title','logo'));
	}
	$return['item']=$item;
	$return['worker']=$worker;
	$return['store']=$store;
	set_html('title','商品核销');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	$update=array('lasttimes -='=>1);
	if($item['lasttimes']==1){
		$update['status']=2;
	}
	pdo_update('jy_yiliao_worker_goods_order',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	exi('核销成功！','','jy_yiliao/worker.index/');
	
}
?>